import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

import java.util.List;

/**
 * @author wangweiqiang
 * @version 1.0
 * @date 2021-08-17 12:47
 */
public class GroupTask {
    @Test
    public void testDeploy(){
        final ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        final RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/evection-candidate.bpmn") // 添加bpmn资源
                .name("组任务")
                .disableSchemaValidation()//禁用验证
                .deploy();

        System.out.println(deployment.getName());
    }

    @Test
    public void testStartProcess(){
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final RuntimeService runtimeService = processEngine.getRuntimeService();
        final ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("testCandidate");


        System.out.println(processInstance.getId());
    }


    @Test
    public void completTask(){
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        final TaskService taskService = processEngine.getTaskService();

        final List<Task> taskList = taskService.createTaskQuery().processInstanceId("147501").list();

        final Task task = taskList.get(0);

        taskService.complete(task.getId());
    }

    @Test
    public void testQueryGroupTask(){
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final TaskService taskService = processEngine.getTaskService();

        final List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("lisi").processDefinitionKey("testCandidate").list();

        for (Task task : tasks) {
            System.out.println("----------------------------");
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }
    }


    @Test
    public void testClaimTask(){
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final TaskService taskService = processEngine.getTaskService();

        final List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("lisi").processDefinitionKey("testCandidate").list();

        final Task task = tasks.get(0);
        System.out.println("李四拾取任务");
        //李四拾取任务
        taskService.claim(task.getId(),"lisi");

    }

    @Test
    public void testQueryUserTask(){
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final TaskService taskService = processEngine.getTaskService();

        //查询李四负责的任务
        final List<Task> list = taskService.createTaskQuery().taskAssignee("lisi").list();
        System.out.println("李四负责的任务:");
        for (Task t : list) {
            System.out.println(t.getName());
            System.out.println(t.getId());
        }

        //查询李四为候选人的任务
        final List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("lisi").list();
        System.out.println("李四为候选人的任务:");
        for (Task t : tasks) {
            System.out.println(t.getName());
            System.out.println(t.getId());
        }
    }

    @Test
    public void testReturnTask(){
        //查询李四为候选人的任务
        final ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        final TaskService taskService = processEngine.getTaskService();

        final List<Task> tasks = taskService.createTaskQuery().taskAssignee("lisi").processDefinitionKey("testCandidate").list();

        final Task task = tasks.get(0);

        //todo 判断此任务是否由其他候选人

        //归还任务
        taskService.setAssignee(task.getId(),null);//也可以指定其他候选人

        System.out.println("李四归还任务");
    }

}
